/**
 * 
 */
package dt.inlee.system.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionContext;
import dt.inlee.common.constant.CommonConstant;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.domain.DesktopDefend;
import dt.inlee.domain.SelfDesktopDefend;
import dt.inlee.domain.SysUser;
import dt.inlee.system.dao.DesktopDefendDao;
import dt.inlee.system.dao.SelfDesktopDefendDao;
import dt.inlee.system.dao.SysUserDao;
import dt.inlee.system.vo.SysUserLoginBean;

/**
 * @功能说明：桌面管理业务层Service
 * @作者： xgyin
 * @创建日期： 2010-12-22
 */
@Service("desktopDefendService")
public class DesktopDefendService {
	@Resource(name = "desktopDefendDao")
	private DesktopDefendDao desktopDefendDao;
	@Resource(name = "sysUserDao")
	private SysUserDao sysUserDao;
	@Resource(name = "selfDesktopDefendDao")
	private SelfDesktopDefendDao selfDesktopDefendDao;

	/**
	 * 通过桌面管理ID删除桌面管理实体
	 * @param desktopDefendId
	 * @throws FrameworkException
	 */
	@Transactional
	public void deleteDesktopDefendById(String desktopDefendId) throws FrameworkException {
		this.desktopDefendDao.deleteById(DesktopDefend.class, desktopDefendId);
	}

	@Transactional
	public void deleteDesktopDefends(String[] desktopDefendIds) throws FrameworkException {
		for (String desktopDefendId : desktopDefendIds) {
			this.deleteDesktopDefendById(desktopDefendId);
		}
	}

	/**
	 * 通过桌面管理ID获取桌面管理实体
	 * @param desktopDefendId
	 * @return
	 * @throws FrameworkException
	 */
	public DesktopDefend findDesktopDefendById(String desktopDefendId) throws FrameworkException {
		return (DesktopDefend) this.desktopDefendDao.get(DesktopDefend.class, desktopDefendId);
	}

	/**
	 * 保存桌面管理
	 * @param desktopDefend
	 * @throws FrameworkException
	 */
	@Transactional
	public void saveDesktopDefend(DesktopDefend desktopDefend) throws FrameworkException {
		this.desktopDefendDao.save(desktopDefend);

	}

	/**
	 * 修改桌面管理
	 * @param desktopDefend
	 * @throws FrameworkException
	 */
	@Transactional
	public void updateDesktopDefend(DesktopDefend desktopDefend) throws FrameworkException {
		this.desktopDefendDao.update(desktopDefend);

	}

	/**
	 * 根据条件获取桌面管理总记录数
	 * @param title
	 * @param divId
	 * @return
	 * @throws FrameworkException
	 */
	public int getDesktopDefendTotalRows(String title, String divId) throws FrameworkException {
		return this.desktopDefendDao.getDesktopDefendTotalRows(title, divId);
	}

	/**
	 * 根据条件获取桌面管理记录
	 * @param title
	 * @param divId
	 * @param curPage
	 * @param pageSize
	 * @return
	 * @throws FrameworkException
	 */
	public List<DesktopDefend> findAllDesktopDefends(String title, String divId, int curPage, int pageSize)
			throws FrameworkException {
		return this.desktopDefendDao.findAllDesktopDefends(title, divId, curPage, pageSize);
	}

	/**
	 * 通过桌面管理英文名称和中文名称查找桌面管理
	 * @param name
	 * @param cnName
	 * @return
	 * @throws FrameworkException
	 */
	public DesktopDefend findDesktopDefendyByName(String title, String divId) throws FrameworkException {
		return this.desktopDefendDao.findDesktopDefendyByName(title, divId);
	}

	/**
	 * 通过桌面管理ID、英文名称和中文名称查找桌面管理
	 * @param id
	 * @param name
	 * @param cnName
	 * @return
	 * @throws FrameworkException
	 */
	public DesktopDefend findDesktopDefendByCon(String id, String title, String divId) throws FrameworkException {
		return this.desktopDefendDao.findDesktopDefendByCon(id, title, divId);

	}

	/**
	 * 获取自己的桌面业务模块
	 * @return
	 * @throws FrameworkException
	 */
	public List<DesktopDefend> findSelfDesktopDefends() throws FrameworkException {
		SysUserLoginBean loginUser = (SysUserLoginBean) ActionContext.getContext().getSession().get(
				CommonConstant.SESSION_USER);
		return this.desktopDefendDao.findSelfDesktopDefends(loginUser.getId()+"");
	}

	/**
	 * 设置自己的桌面菜单
	 * @param desktopDefendIds
	 * @throws FrameworkException
	 */
	@Transactional
	public void setSelfDesktopDefends(String[] desktopDefendIds) throws FrameworkException {
		SysUserLoginBean loginUser = (SysUserLoginBean) ActionContext.getContext().getSession().get(
				CommonConstant.SESSION_USER);
		SysUser sysUser = this.sysUserDao.getUserById(loginUser.getId());
		this.selfDesktopDefendDao.deleteSelfDesktopDefendsByLoginUserId(loginUser.getId()+"");
		if (BaseUtil.isEmpty(desktopDefendIds))
			return;
		int len = desktopDefendIds.length;
		for (int i = 0; i < len; i++) {
			String desktopDefendId = desktopDefendIds[i];
			DesktopDefend defend = (DesktopDefend) this.desktopDefendDao.get(DesktopDefend.class, desktopDefendId);
			SelfDesktopDefend self = new SelfDesktopDefend();
			if (i % 2 == 0)
				self.setDefaultPosition("1");
			else
				self.setDefaultPosition("2");
			self.setDefendUser(sysUser);
			self.setDesktopDefend(defend);
			this.selfDesktopDefendDao.save(self);
		}
	}

	/**
	 * 获取自己主页的桌面模块
	 * @return
	 * @throws FrameworkException
	 */
	public List<SelfDesktopDefend> findSelfDesktopCenters() throws FrameworkException {
		SysUserLoginBean loginUser = (SysUserLoginBean) ActionContext.getContext().getSession().get(
				CommonConstant.SESSION_USER);
		return this.selfDesktopDefendDao.findSelfDesktopCenters(loginUser.getId());
	}

	/**
	 * 改变桌面区域块的位置
	 * @param placeId
	 * @throws FrameworkException
	 */
	@Transactional
	public void setDesktopPositionds(String placeId) throws FrameworkException {
		String[] places = placeId.split("\\|");
		for (int i = 1; i <= places.length; i++) {
			String widges = places[i - 1];
			String[] widgePlaces = widges.split("=");
			if (widgePlaces.length != 2)
				continue;
			String position = widgePlaces[0];
			String selfDesktopIds = widgePlaces[1];
			if (!BaseUtil.isEmpty(selfDesktopIds)) {
				String[] selfDesktopIdStr = selfDesktopIds.split(",");
				for (int j = 1; j <= selfDesktopIdStr.length; j++) {
					String selfDesktopId = selfDesktopIdStr[j - 1];
					SelfDesktopDefend self = (SelfDesktopDefend) this.selfDesktopDefendDao.get(SelfDesktopDefend.class,
							selfDesktopId);
					self.setOrderNo(j);
					self.setDefaultPosition(position);
					this.selfDesktopDefendDao.update(self);
				}
			}

		}

	}

	/**
	 * 删除自己桌面的桌面项
	 * @param selfDesktopDenfendId
	 * @throws FrameworkException
	 */
	@Transactional
	public void deleteSelfDesktopDefendById(String selfDesktopDenfendId) throws FrameworkException {
		this.selfDesktopDefendDao.deleteById(SelfDesktopDefend.class, selfDesktopDenfendId);
	}
}
